package de.rac.vaadin.framework.ui;
/*******************************************************************************
 * Data for each edit form field: this is a base class<br>
 * @author Volodymyr Dobryvechir (dobrivecher@yahoo.com)
 * @version 0.2 31.05.2012  <br>
 ******************************************************************************/
import java.io.*;
import com.vaadin.data.Container.Filter;
import com.vaadin.data.Validator;
import com.vaadin.ui.Field;
import java.util.*;
import com.vaadin.data.Item;
public class EditFormField implements Serializable {

	private static final long serialVersionUID = 1L;
	protected String columnName,caption,origCaption;
    protected int hashcode;
    protected boolean required=false,generated=false;
    protected ArrayList<Validator> validators;
    protected Long visibilityMask=0L;
    protected int visibilityMax=0;
    protected ArrayList<Filter> subDependFilters = new ArrayList<Filter>();
    protected ArrayList<Integer> subDependNumber = new ArrayList<Integer>();
    protected ArrayList<EditFormField> subDependField = new ArrayList<EditFormField>();
    protected ArrayList<EditFormField> mainDependField = new ArrayList<EditFormField>();
    protected Item currentItem;
    protected Field currentField;
    public EditFormField(String columnName)
    {
    	this.columnName = columnName;
    	hashcode = columnName.hashCode();
    	required=false;
    	validators=new ArrayList<Validator>();
    }
    public EditFormField(String columnName,String caption,ResourcePool resource)
    {
    	this(columnName);
    	this.origCaption = caption;
    	this.caption = resource.getTranslation(caption);
    }
    public EditFormField(String columnName,String caption,boolean required,ResourcePool resource)
    {
    	this(columnName,caption,resource);
    	this.required = required;
    }
    public int addMainVisibilityObject(EditFormField o)
    {
    	mainDependField.add(o);
    	int n=visibilityMax++;
    	return n;
    }
    public void addSubVisibilityObject(EditFormField o,Filter filter,int nr)
    {
    	subDependFilters.add(filter);
    	subDependField.add(o);
    	subDependNumber.add(new Integer(nr));
    }
    public void setFieldVisible(boolean isOn)
    {
       if (currentField!=null) currentField.setVisible(isOn);	
    }
    public void fireVisibility(int nr,boolean isOn)
    {
    	Long msk = 1L<<nr;
    	if (!isOn) visibilityMask |= msk;
    	else visibilityMask &= ~msk;
    	boolean toBeVisible = visibilityMask==0;
        setFieldVisible(toBeVisible);
    }
    
    public void notifyOfLocaleChange(ResourcePool resource,boolean activeEdit)
    {
    	caption = resource.getTranslation(origCaption);
    	if (validators!=null)
    	{
    		int n = validators.size();
    		for(int i=0;i<n;i++) 
    		{
    			Object a = validators.get(i);
    			if (a instanceof TranslatedValidator)
    			{
    				((TranslatedValidator)a).setProperTranslation(resource);
    			}
    		}
    	}
    	if (activeEdit && currentField!=null) currentField.setCaption(caption);
    }
    public void checkOwnVisibility()
    {
    	int n=mainDependField.size();
    	for(int i=0;i<n;i++)
    	{
    		EditFormField field = mainDependField.get(i);
    		field.checkVisibilityDependencies(currentItem);
    	}
    }
    public void checkVisibilityDependencies(Item item)
    {
    	int n=subDependField.size();
    	for(int i=0;i<n;i++)
    	{
    		Filter filter = subDependFilters.get(i);
    		EditFormField field = subDependField.get(i);
    		int nr = subDependNumber.get(i).intValue();
    		boolean isOn=filter.passesFilter(item, item);
    		field.fireVisibility(nr, isOn);
    	}
    }
    public void checkVisibilityDependenciesForCurrentItem()
    {
    	checkVisibilityDependencies(currentItem);
    }
	@Override
	public int hashCode() {
		return hashcode;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (obj instanceof String)
		{
			return obj.equals(columnName);
		}
		if (!(obj instanceof EditFormField))
			return false;
		EditFormField other = (EditFormField) obj;
		if (columnName == null) {
			if (other.columnName != null)
				return false;
		} else if (!columnName.equals(other.columnName))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "EditFormField [columnName=" + columnName + "]";
	}
    public void setCaptionName(String caption)
    {
       	this.caption = caption;
    }
    public String getCaptionName() { return caption; }
    public boolean isRequired() { return required; }
    public void setRequired(boolean required)
    {
    	this.required = required;
    }
    public boolean isGenerated() { return generated;}
    public int getValidatorAmount() { return validators.size(); }
    public Validator getValidator(int index) { return validators.get(index); }
    public void addValidator(Validator validater) { validators.add(validater);}
    public void addValidator(String message,ResourcePool resource,TranslatedValidator.TranslatedValidatorGetter getter)
    {
    	addValidator(new TranslatedValidator(message,resource,getter));
    }
    public Field getFieldGenerated(Item item) {currentItem=item; return null; }
    public void setCurrentField(Field field) {currentField=field;this.checkOwnVisibility();}
    
}
